﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Foxit.PDF;
using Foxit.PDF.IO;
using Foxit.PDF.PageElements;
using Sustainalytics.Entities.RiskRatings;
using Sustainalytics.Reporting.Framework;
using Sustainalytics.Entities;

namespace Sustainalytics.RiskRatings.Reporting.Headers_and_Footers
{
    class OverviewPageHeader : PageElement
    {
        public float Left { get; set; }
        public float Top { get; set; }
        public float Width { get; set; }
        public float Height { get; set; }
        public string CompanyName { get; set; }
        public string SubPeerGroup { get; set; }
        public string Country { get; set; }
        public string ExchangeTicker { get; set; }
        public float UnmanagedRiskValue { get; set; }
        public string Assessment { get; set; }
        public List<RiskRatingCluster> RiskRatingClusterList { get; set; }
        public DateTime LastUpdate { get; set; }
        public ReportType ReportType { get; set; }
        public bool IsFinalSection { get; set; }

        private Group Elements()
        {
            //left
            var elements = new Group();
            var middleSpace = 25;
            var columnWidth = (Width - middleSpace) / 2;

            string reportTitle = "Sustainalytics ESG Risk Rating Report".ToUpper();

            if (ReportType == ReportType.SummaryRiskRatingsReport)
                reportTitle = "Sustainalytics ESG Risk Rating Summary Report".ToUpper();

            float currentYOffset = Top;

            elements.Add(new Label(reportTitle, Left, currentYOffset, columnWidth, 9, Fonts.MontserratBold, 9, TextAlign.Left, RgbColor.Black));
            currentYOffset += 15;

            var companyNameLenght = Fonts.RobotoMedium.GetTextWidth(CompanyName, 20);
            if (companyNameLenght > columnWidth)
            {
                float companyNameFontSize = 19.5f;
                var textArea = new TextArea(CompanyName, Left, currentYOffset, columnWidth, companyNameFontSize * 2 + 10, Fonts.RobotoMedium, companyNameFontSize, TextAlign.Left, RgbColor.Black);
                while (textArea.GetRequiredHeight() > 50)
                {
                    companyNameFontSize -= 0.5f;
                    textArea = new TextArea(CompanyName, Left, currentYOffset, columnWidth, companyNameFontSize * 2 + 10, Fonts.RobotoMedium, companyNameFontSize, TextAlign.Left, RgbColor.Black);
                }
                elements.Add(textArea);
                currentYOffset += 50;
            }
            else
            {
                currentYOffset += 15;
                elements.Add(new Label(CompanyName, Left, currentYOffset, columnWidth, 20f, Fonts.RobotoMedium, 20.5f, TextAlign.Left, RgbColor.Black));
                currentYOffset += 35;
            }


            string companyInfo = string.Format("{0} | {1} | {2}", SubPeerGroup, Country, ExchangeTicker);
            elements.Add(new Label(companyInfo, Left, currentYOffset, columnWidth, 8, Fonts.RobotoRegular, 8, TextAlign.Left, RgbColor.Black));

            if (!IsFinalSection)
            {
                //right
                Left = columnWidth + middleSpace;
                currentYOffset = Top;
                float value1 = UnmanagedRiskValue;


                string dateText = LastUpdate.ToString("MMM d, yyyy");
                float dateTextWidth = Fonts.RobotoBlack.GetTextWidth(dateText, 7);
                elements.Add(new Label(dateText, Left, currentYOffset, columnWidth, 7, Fonts.RobotoBlack, 7, TextAlign.Right, RgbColor.Black));
                elements.Add(new Label("Date", Left, currentYOffset, columnWidth - dateTextWidth - 3, 7, Fonts.RobotoRegular, 7, TextAlign.Right, RgbColor.Black));
                currentYOffset += 30f;


                RiskRating_CustomBulletChart chartObj = new RiskRating_CustomBulletChart();
                chartObj.Top = currentYOffset;
                chartObj.Left = Left + 15f;
                chartObj.Width = columnWidth;
                chartObj.Value = value1;
                chartObj.Assessment = Assessment;
                chartObj.RiskRatingClusterList = RiskRatingClusterList;
                chartObj.BarHeight = 15;
                elements.Add(chartObj);
            }
            return elements;
        }

        public override void Draw(PageWriter writer)
        {
            var elements = Elements();

            writer.Write_q_(true);
            elements.Draw(writer);
            writer.Write_Q(true);
        }
    }
}
